// $Id$

using System;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NUnit.Framework;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Caching;
using Northwind;
using Northwind.DomainModel.EnterpriseLibrary;

namespace Salamanca.NUnit
{
    public class NorthwindTestBase
    {
        [TestFixtureSetUp]
        protected virtual void FixtureSetUp()
        {
        }

        [TestFixtureTearDown]
        protected virtual void FixtureTearDown()
        {
            if (_Db!=null)
                _Db.Dispose();
        }

        [SetUp]
        protected virtual void SetUp()
        {
            _Transaction=new TransactionScope();
        }

        [TearDown]
        protected virtual void TearDown()
        {
            _Transaction.Dispose();

            CacheManager.Clear();
        }

        protected Database Database
        {
            get
            {
                return Db.Database;
            }
        }

        protected Salamanca.DataAccess.ICacheManager CacheManager
        {
            get
            {
                if (_CacheManager==null)
                    _CacheManager=new CacheManagerAdapter(CacheFactory.GetCacheManager());
                return _CacheManager;
            }
        }

        protected DataMapperCollection DataMappers
        {
            get
            {
                if (_DataMappers==null)
                    _DataMappers=Mappers.CreateDataMapperCollection(Database, CacheManager);
                return _DataMappers;
            }
        }

        protected NorthwindDb Db
        {
            get
            {
                if (_Db==null)
                    _Db=new NorthwindDb(NorthwindDatabaseType.SqlClient);
                return _Db;
            }
        }

        private DataMapperCollection _DataMappers;
        private Salamanca.DataAccess.ICacheManager _CacheManager;
        private TransactionScope _Transaction;
        private NorthwindDb _Db;
    }
}
